package com.zhkj.txg.module.product.vm

import android.app.Application
import androidx.lifecycle.MutableLiveData
import com.zhkj.lib.base.BaseViewModel
import com.zhkj.lib.http.HttpResponse
import com.zhkj.lib.http.ViewModelSubscribeListener
import com.zhkj.lib.utils.AccountManager
import com.zhkj.txg.module.product.entity.RankProductResponse
import com.zhkj.txg.module.product.model.ProductManager

class RankViewModel(application: Application) : BaseViewModel(application) {

    private val manager = ProductManager()
    val rankProductLiveData = MutableLiveData<HttpResponse<RankProductResponse>>()

    var catId: Long = 0

    private var page = 1
    private val numb = 20

    fun rankProduct(restart: Boolean = false) {
        val listener = object : ViewModelSubscribeListener<RankProductResponse>(rankProductLiveData) {
            override fun onSuccess(resp: RankProductResponse) {
                resp.hasMore = resp.data.size >= numb
                resp.loadMore = page > 1
                super.onSuccess(resp)
                page += 1
            }
        }
        if (restart) page = 1
        val rankProduct = manager.rankProduct(AccountManager.getToken(), catId, page, numb, listener)
        dispose.add(rankProduct)
    }
}